Merge pull request #812 from younata/master

Add 'unescapeHTML' functionality to the javascript agent.

Andrew Cantino 9 years ago
parent
commit
1a43bd2907
2 changed files with 26 additions and 0 deletions
  1. 12 0
      app/models/agents/java_script_agent.rb
  2. 14 0
      spec/models/agents/java_script_agent_spec.rb

+ 12 - 0
app/models/agents/java_script_agent.rb

@@ -21,6 +21,8 @@ module Agents
21 21
       * `this.options(key)`
22 22
       * `this.log(message)`
23 23
       * `this.error(message)`
24
+      * `this.escapeHtml(htmlToEscape)`
25
+      * `this.unescapeHtml(htmlToUnescape)`
24 26
     MD
25 27
 
26 28
     def validate_options
@@ -102,6 +104,8 @@ module Agents
102 104
           memory.to_json
103 105
         end
104 106
       end
107
+      context["escapeHtml"] = lambda { |a, x| CGI.escapeHTML(x) }
108
+      context["unescapeHtml"] = lambda { |a, x| CGI.unescapeHTML(x) }
105 109
 
106 110
       context.eval(code)
107 111
       context.eval("Agent.#{js_function}();")
@@ -158,6 +162,14 @@ module Agents
158 162
           doError(message);
159 163
         }
160 164
 
165
+        Agent.escapeHtml = function(html) {
166
+          return escapeHtml(html);
167
+        }
168
+
169
+        Agent.unescapeHtml = function(html) {
170
+          return unescapeHtml(html);
171
+        }
172
+
161 173
         Agent.check = function(){};
162 174
         Agent.receive = function(){};
163 175
       JS

+ 14 - 0
spec/models/agents/java_script_agent_spec.rb

@@ -162,6 +162,20 @@ describe Agents::JavaScriptAgent do
162 162
       end
163 163
     end
164 164
 
165
+    describe "escaping and unescaping HTML" do
166
+      it "can escape and unescape html with this.escapeHtml and this.unescapeHtml in the javascript environment" do
167
+        @agent.options['code'] = 'Agent.check = function() { this.createEvent({ escaped: this.escapeHtml(\'test \"escaping\" <characters>\'), unescaped: this.unescapeHtml(\'test &quot;unescaping&quot; &lt;characters&gt;\')}); };'
168
+        @agent.save!
169
+        expect {
170
+          expect {
171
+            @agent.check
172
+          }.not_to change { AgentLog.count }
173
+        }.to change { Event.count}.by(1)
174
+        created_event = @agent.events.last
175
+        expect(created_event.payload).to eq({ 'escaped' => 'test &quot;escaping&quot; &lt;characters&gt;', 'unescaped' => 'test "unescaping" <characters>'})
176
+      end
177
+    end
178
+
165 179
     describe "getting incoming events" do
166 180
       it "can access incoming events in the JavaScript enviroment via this.incomingEvents" do
167 181
         event = Event.new